//
//  HelpViewController.m
//  Cheirapsis
//
//  Created by zhouhaie on 15/11/3.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "HelpViewController.h"
#import "Define.h"
#import "Request.h"
#import "UUAVAudioPlayer.h"

@interface HelpViewController ()<UUAVAudioPlayerDelegate>{
    UUAVAudioPlayer *audio;
    BOOL contentVoiceIsPlaying;
    NSData *songData;
    NSArray * arr;
}

@end

@implementation HelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self data];
    self.title = @"盲人求助";
}
-(void)data{
    arr = @[
            @{
                @"voice":@"语音消息1"
                },
            @{@"voice":@"语音消息2"
              }
            ];
    [[Request sharedInstance] getVoiceHelp:nil content:^(NSArray *array) {
        _dataSource = array;
        [_tableView reloadData];
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
////header高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 300;
//}
//cell数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arr.count;
}
//绘制cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"myCell";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        
        
        
        NSDictionary *dic=[arr objectAtIndex:indexPath.row];
        cell.textLabel.text=dic[@"voice"];
        
    }
    UIButton * voice = [[UIButton alloc]initWithFrame:CGRectMake(265*F_WIDTH, 18*F_HEIGHT, 38*F_WIDTH, 35*F_HEIGHT)];
    
    [voice setImage:[UIImage imageNamed:@"播放器"] forState:UIControlStateNormal];
    voice.tag = indexPath.row;
    [voice addTarget:self action:@selector(playVoice:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:voice];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15*F_WIDTH, 79*F_HEIGHT, WIDTH-15*F_WIDTH, 1)];
    label.backgroundColor = ALL233COROL;
    [cell addSubview:label];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
 
    return cell;
}

- (void)playVoice:(UIButton *)btn{
    if (!contentVoiceIsPlaying) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VoicePlayHasInterrupt" object:nil];
        contentVoiceIsPlaying = YES;
        audio = [UUAVAudioPlayer sharedInstance];
        audio.delegate = self;
        [audio playSongWithData:songData];
    }else{
        [self UUAVAudioPlayerDidFinishPlay];
    }
    NSString *voiceURL = [_dataSource objectAtIndex:btn.tag];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:voiceURL]];
    
    if (data) {
        [audio playSongWithData:data];
    }
}

- (void)UUAVAudioPlayerBeiginPlay
{
    //开启红外线感应
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    
}
- (void)UUAVAudioPlayerDidFinishPlay
{
    //关闭红外线感应
    [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
    contentVoiceIsPlaying = NO;
    
    [[UUAVAudioPlayer sharedInstance]stopSound];
}

@end
